<template>
  <el-container>
    <el-main>
      <el-button type="primary" @click="$router.push({ path: '/addrole' })">添加</el-button>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID" min-width="80"></el-table-column>
        <el-table-column prop="name" label="VIP" width="288"></el-table-column>
        <el-table-column prop="expire_days" label="天数" width="288"></el-table-column>
        <el-table-column prop="charge" label="价格" width="288"></el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button @click="del(scope.row.id)" type="text" style="color: red">删除</el-button>
            <el-button type="text" @click="edit(scope.row.id)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>
</template>

<script>
import { roles, delRole } from '@/network/operate'

export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    async getRoles() {
      const { data } = await roles()
      this.tableData = data.data
    },
    del(id) {
      this.$confirm('确认操作?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { status, message } = await delRole(id)
        if(status === 0) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getRoles()
        }else {
          this.$message({ type: 'error', message})
        }
      }).catch(() => {})
    },
    edit(id) {
      this.$router.push({ path: '/editrole', query: { id } })
    }
  },
  created() {
    this.getRoles()
  }
}
</script>

<style lang="scss" scoped>
.el-container {
  padding: 20px 6px 28px 20px;
  background-color: #f1f2f9;
  .el-main {
    padding: 28px;
    border-radius: 12px;
    background-color: #fff;
    .el-table {
      margin-top: 28px;
    }
  }
}
</style>